﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using System.Linq;

namespace CloudStorageLight.Core.Tests
{
    [TestClass]
    public class FileBlobContainerTest
    {
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) 
        {
           
            Directory.CreateDirectory("SUB1");
            Directory.CreateDirectory("SUB1\\SUb2");
            File.CreateText("SUB1\\TEST1.TXT");
            File.CreateText("SUB1\\TEST2.TXT");
            File.CreateText("SUB1\\SUB2\\TEST3.TXT");
        }

        [TestMethod]
        public void URLとWindowsファイル名変換()
        {
            var blob = new FileBlobAdapter("/", "c:\\temp");
            var winpath = blob.ConvertUrlPathToWindowsPath("/test/SUBX");
            var uriPath = blob.ConvertWindowsPathToUrlPath(winpath);

            Assert.AreEqual("/test/SUBX", uriPath);
        }

        [TestMethod]
        public void ListBlobTest()
        {
            var blob = new FileBlobAdapter("/", Path.GetFullPath("SUB1\\"));

            var result = blob.GetFilesAsync("/",100,true).Result;
            Assert.AreEqual(5, result.Count);
        }

        [TestMethod]
        public void ExistsAsyncTest()
        {
            var blob = new FileBlobAdapter("/", Path.GetFullPath("SUB1\\"));

            var result = blob.ExistsAsync("/TEST1.TXT").Result;
            Assert.IsTrue(result);
            var result2 = blob.ExistsAsync("/TEST9.TXT").Result;
            Assert.IsFalse(result2);
        }

        //[TestMethod]
        //public void UCNTest()
        //{
        //    var blob = new FileBlobAdapter(null, "/", "\\\\localhost\\c$\\");
        //    var result = blob.ListBlob(null).ToArray();
        //}

        [TestMethod]
        public void CopyAsyncTest()
        {
            var blob = new FileBlobAdapter( "/", Path.GetFullPath("SUB1\\"));

            var from = new BlobFile(blob.Parent, "/Test1.TXT");
            try
            {
                var result = blob.ExistsAsync("/TEST10.TXT").Result;
                Assert.IsFalse(result);
                blob.CopyAsync("/TEST10.TXT", from).Wait();

                result = blob.ExistsAsync("/TEST10.TXT").Result;
                Assert.IsTrue(result);

            }
            finally
            {
                blob.DeleteAsync(new BlobFile(blob.Parent, "/TEST10.TXT")).Wait();
            }
        }
    }
}
